clear all; clc; close all;
N = 1000;
x1 = 2*rand(N,1)-1;
x2 = 2*rand(N,1)-1;

s = @(x) 1./(1+exp(-x));

y = s(sin(8*x1) + x1 - x2 + 0.2 + 0.5*randn(N,1));
y = round(y);


X = [x1 x2 ones(N,1)];
w = logreg(X,y,0.01);
y2 = classify(X,w);    

x = linspace(-1,1,100);
I1 = logical( (y==y2) .* (y==0));
I2 = logical( (y==y2) .* (y==1));
I3 = logical( (y~=y2) .* (y==1));
I4 = logical( (y~=y2) .* (y==0));
plot(x1(I1),x2(I1),'O','color','blue');
hold on
plot(x1(I2),x2(I2),'O','color','green');
plot(x1(I3),x2(I3),'O','color','red');
plot(x1(I4),x2(I4),'O','color','black');
plot(x,(-w(1)/w(2))*x-w(3)/w(2),'color','green','linewidth',3);
plot(x,sin(8*x) + x + 0.2,'color',[1 215/255 0],'linewidth',3);
legend('correctly classified as -','correctly classified as +',...
'incorrectly classified as -','incorrectly classified as +',...
'decision boundary of logistic regression','true decision boundary');
axis([-1 1 -1 1])


X = [x1 x2 sin(8*x1) ones(N,1)];
w = logreg(X,y,0.01);
y2 = classify(X,w); 

figure()
plot(x,sin(8*x) + x + 0.2,'color','green','linewidth',3);
I1 = logical( (y==y2) .* (y==0));
I2 = logical( (y==y2) .* (y==1));
I3 = logical( (y~=y2) .* (y==1));
I4 = logical( (y~=y2) .* (y==0));
plot(x1(I1),x2(I1),'O','color','blue');
hold on
plot(x1(I2),x2(I2),'O','color','green');
plot(x1(I3),x2(I3),'O','color','red');
plot(x1(I4),x2(I4),'O','color','black');

plot(x,-w(1)*x/w(2) -w(3)*sin(8*x)/w(2) - w(4)/w(2),'color','green','linewidth',3);
plot(x,sin(8*x) + x + 0.2,'color',[1 215/255 0],'linewidth',3);
legend('correctly classified as -','correctly classified as +',...
'incorrectly classified as -','incorrectly classified as +',...
'decision boundary of logistic regression','true decision boundary');
axis([-1 1 -1 1])
